JBoss Community Archive (Read Only)

Errai

Client-Side Bean Manager

It may be necessary at times to manually obtain instances of beans managed by Errai IOC from outside the container managed scope or creating a hard dependency from your bean. Errai IOC provides a simple client-side bean manager for handling these scenarios: org.jboss.errai.ioc.client.container.ClientBeanManager.

As you might expect, you can inject a bean manager instance into any of your managed beans. If you use Errai IOC in its default mode you will need to inject the synchronous bean manager (org.jboss.errai.ioc.client.container.SyncBeanManager).

If you have asynchronous IOC mode enabled simply inject the asynchronous bean manager (org.jboss.errai.ioc.client.container.async.AsyncBeanManager) instead. Asynchronous IOC brings support for code splitting. That means that any bean annotated with @LoadAsync can be compiled into a separate JavaScript file that's downloaded when the bean is first needed on the client.

Injecting the client-side bean manager
public MyManagedBean {
  @Inject SyncBeanManager manager;

  // class body
}

If you need to access the bean manager outside a managed bean, such as in a unit test, you can access it by calling org.jboss.errai.ioc.client.container.IOC.getBeanManager()

Looking up beans

Looking up beans can be done through the use of the lookupBeans() method. Here's a basic example:

Example lookup of a bean
public MyManagedBean {
  @Inject SyncBeanManager manager;

  public void lookupBean() {
    IOCBeanDef<SimpleBean> bean = manager.lookupBean(SimpleBean.class);

    if (bean != null) {
      // get the instance of the bean
      SimpleBean inst = bean.getInstance();    
    }    
  }
}

In this example we lookup a bean class named SimpleBean. This example will succeed assuming that SimpleBean is unambiguous. If the bean is ambiguous and requires qualification, you can do a qualified lookup like so:

Looking up beans with qualifiers
MyQualifier qual = new MyQualifier() {
  public annotationType() {
    return MyQualifier.class;
  }
}

MyOtherQualifier qual2 = new MyOtherQualifier() {
  public annotationType() {
    return MyOtherQualifier.class;
  }
}

// pass qualifiers to ClientBeanManager.lookupBeans
IOCBeanDef<SimpleBean> bean = beanManager.lookupBean(SimpleBean.class, qual, qual2);

In this example we manually construct instances of qualifier annotations in order to pass it to the bean manager for lookup. This is a necessary step since there's currently no support for annotation literals in Errai client code.

Availability of beans

Not all beans that are available for injection are available for lookup from the bean manager by default. Only beans which are explicitly scoped are available for dynamic lookup. This is an intentional feature to keep the size of the generated code down in the browser.

JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-10 12:34:51 UTC, last content change 2013-10-17 15:13:23 UTC.